// 服务器配置信息
const server = {
	host: '39.105.85.75', // 服务器ip
	port: '22', // 端口一般默认22
	username: 'root', // 用户名
	password: '', // 密码
	serverPath: '/fuwubao/html/', // 服务器路径（结尾加/）
	packageName: 'manage', // 上传到服务器的位置
	localPath: './manage/' // 本地打包文件路径
}
// 引入scp2
const client = require('scp2');
const _ora = require('ora');

const spinner = _ora('正在发布到测试服务器...');

// 创建shell脚本
const Client = require('ssh2').Client;
const conn = new Client();

console.log('正在建立连接');
conn.on('ready', function() {
	console.log('已连接')
	if (!server.packageName) {
		console.log('连接已关闭');
		conn.end()
		return false;
	}
	// 这里我拼接了放置服务器资源目录的位置 ，首选通过rm -rf删除了这个目录下的文件
	conn.exec('rm -rf ' + server.serverPath + server.packageName + '/*', function(err, stream) {
		console.log('删除服务器文件');
		stream.on('close', function(code, signal) {
			console.log('开始上传')
			spinner.start();
			client.scp(server.localPath, {
				"host": server.host,
				"port": server.port,
				"username": server.username,
				"password": server.password,
				"privateKey": `-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAyC7mxtiX0fsMGr04K5pezc1u+kS1/v+dcliA99DWsLu2j+JL
mFQUZ7l/y/4jxErVrDGiQuCEh/NJc7UwnoGz6j6b6nAv5SJN54rhyttL00/Q5aMB
SdnXE7VmlREcs/Wv5EVwC1/3ySSQbqJyBKXAxlprTzaIujejLa/gtQxNyYxLGoW4
KV0DsEipyqonvigLB+xhydtIL1QSlUjpVkvKt3oquV8x1nvEReIdzr41Hy+Xm2Ze
qoCmZd1fGWtBx7rOeb/e2Y1A97aQvtEINauPszvT67IliSzr+1cv368JX+D0azNF
vescgthATMBhyVel8jxW4/gsXCjhrjj3ClKh7QIBIwKCAQEApd235pY0pqtwbe09
K3FHPM7Jqss3tgb+zHU3qML7B3b2WfYEHyEYOK+kZzGom1Py3yHPln+DwRoJqQPX
08qNwhaeccqrV3Qx67w3bZEhkdRrQe12GJ6Nop2eI724sl3a4bXgfnQWc3YRRbnM
L7yJyOvVQaI2xi4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o=
-----END RSA PRIVATE KEY-----
`,
				"path": server.serverPath + server.packageName
			}, err => {
				spinner.stop();
				if (!err) {
					console.log("项目发布完毕，地址：http://39.105.85.75:61500/manage/")
				} else {
					console.log("err", err);
				}
				conn.end(); // 结束命令
			})
		}).on('data', function(data) {
			console.log('STDOUT: ' + data);
		}).stderr.on('data', function(data) {
			console.log('STDERR: ' + data);
		});
	})
}).connect({
	host: server.host,
	port: server.port,
	username: server.username,
	password: server.password,
	privateKey: `-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAyC7mxtiX0fsMGr04K5pezc1u+kS1/v+dcliA99DWsLu2j+JL
mFQUZ7l/y/4jxErVrDGiQuCEh/NJc7UwnoGz6j6b6nAv5SJN54rhyttL00/Q5aMB
SdnXE7VmlREcs/Wv5EVwC1/3ySSQbqJyBKXAxlprTzaIujejLa/gtQxNyYxLGoW4
KV0DsEipyqonvigLB+xhydtIL1QSlUjpVkvKt3oquV8x1nvEReIdzr41Hy+Xm2Ze
qoCmZd1fGWtBx7rOeb/e2Y1A97aQvtEINauPszvT67IliSzr+1cv368JX+D0azNF
vescgthATMBhyVel8jxW4/gsXCjhrjj3ClKh7QIBIwKCAQEApd235pY0pqtwbe09
K3FHPM7Jqss3tgb+zHU3qML7B3b2WfYEHyEYOK+kZzGom1Py3yHPln+DwRoJqQPX
08qNwhaeccqrV3Qx67w3bZEhkdRrQe12GJ6Nop2eI724sl3a4bXgfnQWc3YRRbnM
L7yJyOvVQaI2xi4ZfaBbGasb4YFnLaccJdm870QoTaKzfgtakhfKglf17flKD/FE
9hv+3Hgfbo6qXWpY30hbkJUVPJYvBhYu+wDeH0ncH2ay/tp5ZXNY9ChHddlagTOi
NiK4py1yBDGdqOcl3z9kWjkOcaMj4y7UU4yssqsp2dd8HrKZI1cXWlqwdXM7lLwU
SHkKawKBgQDx5AXoxeyd4Z0GiHLxhi3v5FwL/TggUBcsT2xHXLt6WAQZLCi6pLrK
nsdB4Kw/bj9nWfSDOuMGksr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